package com.hndzyu.yd.vip;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.task.TabAdapter;
import com.hndzyu.yd.widget.SignDialog;
import com.hndzyu.yd.widget.VipDIalog;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/12/19.
 */
public class PDDFragment extends android.support.v4.app.Fragment {
    public static String[] tabTitle={"食品","母婴","美妆","家纺","服饰","鞋包","手机","汽车","家装","更多"};
    public static String [] ids=    { "1",  "4",  "16",  "818",   "14", "1281","1543","2048","1917","0"};
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pdd,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               getActivity().finish();
            }
        });
        view.findViewById(R.id.et_coin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.level > 0) {
                    startActivity(new Intent(getActivity(), SearchActivity.class));
                } else {
                    final VipDIalog vipDIalog = new VipDIalog(getContext());
                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                        @Override
                        public void chooseTrue() {
                            final SignDialog dialog = new SignDialog(getActivity());
                            dialog.setListener(new SignDialog.SignListener() {
                                @Override
                                public void checktrue() {
                                    dialog.dismiss();
                                    ClipboardManager cm = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                                    cm.setText(UserBean.kefu);
                                    Toast.makeText(getContext(), "客服号复制成功", Toast.LENGTH_SHORT).show();
                                }
                            });
                            dialog.show();
                            dialog.showYD();
                            dialog.showImg(R.mipmap.dialog_vip2);
                            vipDIalog.dismiss();
                        }

                        @Override
                        public void chooseFalse() {
                            vipDIalog.dismiss();
                        }
                    });
                    vipDIalog.show();
                    vipDIalog.setContent();
                }
            }
        });
        Banner banner = view.findViewById(R.id.banner);
//        Integer[] images={R.mipmap.banner_pdd,R.mipmap.banner_zhuanpan};
        Integer[] images={R.mipmap.banner_pdd};
        List<Integer> resultList = new ArrayList<>(images.length);
        for (Integer s : images) {
            resultList.add(s);
        }
        banner.setImages(resultList).setImageLoader(new GlideImageLoader()).start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position==1){
                    if (UserBean.level>0) {
                        startActivity(new Intent(getContext(), ZhuanPan.class));
                    }else {
                        final VipDIalog vipDIalog = new VipDIalog(getContext());
                        vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                            @Override
                            public void chooseTrue() {
                                final SignDialog dialog = new SignDialog(getActivity());
                                dialog.setListener(new SignDialog.SignListener() {
                                    @Override
                                    public void checktrue() {
                                        dialog.dismiss();
                                        ClipboardManager cm = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                                        cm.setText(UserBean.kefu);
                                        Toast.makeText(getContext(), "客服号复制成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                dialog.show();
                                dialog.showYD();
                                dialog.showImg(R.mipmap.dialog_vip2);
                                vipDIalog.dismiss();
                            }

                            @Override
                            public void chooseFalse() {
                                vipDIalog.dismiss();

                            }
                        });
                        vipDIalog.show();
                        vipDIalog.setContent();
                    }
                }
            }
        });
        banner.start();
        final TabLayout tab = view.findViewById(R.id.table);
        final ViewPager viewPager = view.findViewById(R.id.vp);
        List<String> names = new ArrayList<>();
        for (int i=0;i<tabTitle.length;i++){
            names.add(tabTitle[i]);
        }
        List<android.support.v4.app.Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            fragments.add(VPFragment.newInstance(ids[i]));
        }
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
        //给ViewPager设置适配器
        viewPager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来。
        tab.setupWithViewPager(viewPager);
        //设置可以滑动
        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
    class GlideImageLoader extends ImageLoader

    {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }
}
